import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

/** 状态定义 */

const store = new Vuex.Store({
  state: {
    memberId: '',
    customerId: '',
    anchorType: '',
    phone: '',
    openId: '',
    roleType: '',
    visitorId: '',
    enterpriseId: '',
    veekrayStatus: '', // -1：未注册  0: 免费  1：付费  2：已过期
    showBuyCard: '',
    nickName: '', // 登录用户昵称
    appInfo: {},
    cartBadge: 0,
    isMethodInitData: {},
    isPerfectionInfo: '',
    previewImageFlag: false // 商品详情sku是否预览图片
  },
  mutations: {
    setMemberId: (state, memberId) => {
      state.memberId = memberId
    },
    setCustomerId: (state, customerId) => {
      state.customerId = customerId
    },
    setAnchorType: (state, anchorType) => {
      state.anchorType = anchorType
    },
    setPhone: (state, phone) => {
      state.phone = phone
    },
    setOpenId: (state, openId) => {
      state.openId = openId
    },
    setRoleType: (state, roleType) => {
      state.roleType = roleType
    },
    setVisitorId: (state, visitorId) => {
      state.visitorId = visitorId
    },
    setEnterpriseId: (state, enterpriseId) => {
      state.enterpriseId = enterpriseId
    },
    setVeekrayStatus: (state, veekrayStatus) => {
      state.veekrayStatus = veekrayStatus
    },
    setMemberNature: (state, memberNature) => {
      state.memberNature = memberNature
    },
    setStoreCardFlag: (state, storeCardFlag) => {
      state.storeCardFlag = storeCardFlag
    },
    setShowBuyCard: (state, showBuyCard) => {
      state.showBuyCard = showBuyCard
    },
    setNickName: (state, nickName) => {
      state.nickName = nickName
    },
    setAppInfo: (state, appInfo) => {
      state.appInfo = appInfo
    },
    setCartBadge: (state, cartBadge) => {
      state.cartBadge = cartBadge
    },
    setIsPerfectionInfo: (state, isPerfectionInfo) => {
      state.isPerfectionInfo = isPerfectionInfo
    },
    setMethodInitData: (state, pageMethod) => {
      if (pageMethod) {
        // 如果没有页面对象
        if (!state.isMethodInitData[pageMethod.page]) {
          state.isMethodInitData[pageMethod.page] = {}
        }
        // 如果没有传入方法，需要清除该页面所有方法的状态
        if (pageMethod.method) {
          state.isMethodInitData[pageMethod.page][pageMethod.method] = pageMethod.value
        } else {
          state.isMethodInitData[pageMethod.page] = {}
        }
      } else {
        // 清除所有页面的数据状态
        state.isMethodInitData = {}
      }
    },
    setPreviewImageFlag: (state, previewImageFlag) => {
      state.previewImageFlag = previewImageFlag
    }
  }
})

export default store
